<div class="container">
    <div id="role_tb">
        #hasPermission("role:add")
        <a href="#" onclick="addRole()" class="easyui-linkbutton" data-options="iconCls:'fa fa-plus'">添加</a>
        #end
    </div>
    <div class="_datagrid">
        <table id="role_dg"></table>
    </div>
</div>
<script>
    var role_dg;
    $(function () {
        role_dg = $('#role_dg').datagrid({
            url: "#(ctx)/admin/roleMenu/roleList",
            fitColumns: true,
            pagination: true,
            rownumbers: true,
            singleSelect: true,
            pageSize: 10,
            pageList: [10, 20, 30],
            idField: 'roleId',
            columns: [
                [{
                    field: 'roleId',
                    width: 100,
                    hidden: true,
                    title: 'id'
                }, {
                    field: 'roleName',
                    title: '角色名称',
                    width: 100
                }, {
                    field: 'roleDesc',
                    title: '角色描述',
                    width: 150
                }, {
                    field: 'createTime',
                    title: 'createTime',
                    width: 100
                }, {
                    field: '操作',
                    title: '操作',
                    width: 120,
                    formatter: function (value, row, index) {
                        var str = ''
                        #hasPermission("role:grantMenu")
                        str += $.formatString('<a onclick="grantRoleMenu({0})" class="fa fa-user-md">分配权限</a>', row.roleId);
                        str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                        #end
                         #hasPermission("role:edit")
                        str += $.formatString('<a onclick="edit({0})" class="fa fa-pencil">修改</a>', row.roleId);
                        str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                        #end
                        #hasPermission("role:del")
                        str += $.formatString('<a onclick="delRole({0})" class="fa fa-remove">删除</a>', row.roleId);
                        #end
                        return str;
                    }
                }]
            ],
            toolbar: '#role_tb'
        });
    })

    /**
     * 给角色分配菜单
     * @param roleId 角色ID
     */
    function grantRoleMenu(roleId) {
        parent.$.modalDialog({
            title: '授权',
            width: 500,
            height: 500,
            href: '#(ctx)/admin/roleMenu/grantPage/' + roleId,
            buttons: [{
                text: '确定',
                handler: function () {
                    parent.$.modalDialog.openner_dataGrid = role_dg;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#roleGrantForm');
                    f.submit();
                }
            }]
        });
    }

    /**
     * 添加角色
     */
    function addRole() {
        parent.$.modalDialog({
            title: "添加角色",
            width: 300,
            height: 200,
            href: "#(ctx)/admin/roleMenu/roleAddPage",
            buttons: [{
                text: '添加',
                handler: function () {
                    parent.$.modalDialog.openner_dataGrid = role_dg;
                    var f = parent.$.modalDialog.handler.find('#roleAddForm');
                    f.submit();
                    return;
                }
            }]
        });
    }
    function edit(id) {
        parent.$.modalDialog({
            title: "编辑用户",
            width: 300,
            height: 300,
            href: "#(ctx)/admin/roleMenu/roleEditPage/" + id,
            buttons: [{
                text: '修改',
                handler: function () {
                    parent.$.modalDialog.openner_dataGrid = role_dg;
                    var f = parent.$.modalDialog.handler.find('#roleEditForm');
                    f.submit();
                    return;
                }
            }]
        });
    }

    function delRole(id) {
        $.get('#(ctx)/admin/roleMenu/roleDel/' + id, function (response) {
            if (response.result) {
                parent.$.messager.alert('提示', response.msg, 'info');
                role_dg.datagrid('reload');
            }
            progressClose();
        }, 'JSON');
    }
</script>